<?php

App::import(array('Sanitize'));

class EquipmentsController extends AppController {
    
    var $actsAs = array('Containable');
    
    var $name = 'Equipments';
    
    var $uses = array('Category','Equipment','District');
    
    var $components = array('Email');        
    
    var $category_id=6;
    
    var $default_model="Equipment";
    
    
    function beforeFilter(){
        parent::beforeFilter();
        
        $this->set("default_model",$this->default_model);
        
        
        /**
         * Get the data from the find('threaded') and pass it to our new function
         */

        $districts = $this->District->find('threaded');
        $districts = $this->drop_down_from_threaded($districts,"id","name",'District');
        $this->set("districts",$districts);
        
        
    }
    
    
    /* Equipments management    */
    
    
    function admin_index() {
        
        $options['recursive'] = 1;
        
        $options['conditions'][$this->default_model.'.status <>']='deleted';
        
        $options['conditions'][$this->default_model.'.category_id']=$this->category_id;
        
        
        
        $this->paginate[$this->default_model]=$options;
        
        $posts = $this->paginate($this->default_model);
        
        
        $this->set(compact('posts'));

    }
    
    
    
    // add Equipment
    
    function admin_add($id=null) {
        
        $model=$this->default_model;
            
        if(!empty($this->data)):
        
            
            // Clean
            foreach($this->data[$this->default_model] as $key => $item):
                if(is_string($item)) 
                    $this->data[$this->default_model][$key] = htmlspecialchars($item, ENT_QUOTES);
            endforeach;
            
            
            
            if($id){
                $this->data[$this->default_model]['id'] = $id;
            }
            
            
            // default values
            $this->data[$this->default_model]['category_id'] = $this->category_id;
            
            if(!$id)
                $this->data[$this->default_model]['created'] = date('Y-m-d H:i:s');
            $this->data[$this->default_model]['modified'] = date('Y-m-d H:i:s');
            
                
            
            
                
                
                
            
            
            $data[$this->default_model]=$this->data[$this->default_model];
            
            $this->$model->set($data);
            
            //pr($this->data);
            
            if ($this->$model->validates()):
                    
                if($this->$model->save($data)) :
                    $this->Session->write('Success',true);
                    $this->Session->setFlash($this->default_model.' Saved!');
                    $this->redirect($this->referer());
                    exit;
                endif;
            endif;
        elseif($id):
            
            $this->$model->id=$id;
            $this->data=$this->$model->read();
            
            $this->set('id',$id);
        
        endif;
        
        
        
        //$this->set('categories',$this->__getCatList());
        
    }
    
    
    // edit Equipment
    
    function admin_edit($id=null) {
        
        if($id==null){
            $this->redirect($this->referer());
            exit;
        }
        
        $this->admin_add($id);
                
        $this->render('admin_add');
        
    }
    
    // delete Equipment
    function admin_delete($id=null) {
    
        $model=$this->default_model;
        
        if($id==null){
            $this->redirect($this->referer());
            exit;
        }
        
        $tmp=$this->$model->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$id)));
        
        
        if($this->$model->delete(array($this->default_model.'.id'=>$id))) :
            $this->Session->write('Success',true);
            $this->Session->setFlash($this->default_model.' deleted sucessfully');
            
        endif;
        
        
        $this->redirect($this->referer());
        exit;
    }
   

    
    function __clean($content) {
        $content = strip_tags($content);
        $content = preg_replace('/ style.*?=.*?(".*?"|\'.*?\')/', '', $content);
        $content = trim ($content);
        $content = $this->restoreTags($content);
        return $content;
    }
    
    
    // Original PHP code by Chirp Internet: www.chirp.com.au
    protected function restoreTags($input) {
        $opened = array();

        // loop through opened and closed tags in order
        if(preg_match_all("/<(\/?[a-z]+)>?/i", $input, $matches)) {
            foreach($matches[1] as $tag) {
                if(preg_match("/^[a-z]+$/i", $tag, $regs)) {
                    // a tag has been opened
                    if(strtolower($regs[0]) != 'br') $opened[] = $regs[0];
                } elseif(preg_match("/^\/([a-z]+)$/i", $tag, $regs)) {
                    // a tag has been closed
                    unset($opened[array_pop(array_keys($opened, $regs[1]))]);
                }
            }
        }

        // close tags that are still open
        if($opened) {
        $tagstoclose = array_reverse($opened);
        foreach($tagstoclose as $tag) $input .= "</$tag>";
    }

    return $input;
    }
    
     
}
?>
